from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.models import User
from djiape.entities.models import Company, Person, Currency
from djiape.products.models import Product, ListOfPrices, Family
from djiape.accounting.models import Account as GlAccount, Journal_type
from djiape.inventory.models import Location
from django.contrib import admin

class Salesman(Person):
    create_date = models.DateTimeField(_('create date'), auto_now_add=True, editable=False)
    class Meta:
        verbose_name = _('salesman')
        verbose_name_plural = _('salesmen')
    
admin.site.register(Salesman)
    
class LineOfBusiness(models.Model):
    name = models.CharField(_('name'), max_length=30)
    def __unicode__(self):
        return self.name
    class Meta:
        ordering = ["name"]
        verbose_name = _('line of business')
        verbose_name_plural = _('lines of business')
    
admin.site.register(LineOfBusiness)
    
class Zone(models.Model):
    name = models.CharField(_('name'), max_length=30)
    def __unicode__(self):
        return self.name
    class Meta:
        ordering = ["name"]
        verbose_name = _('zone')
        verbose_name_plural = _('zones')
    
admin.site.register(Zone)
    
class ClientType(models.Model):
    name = models.CharField(_('name'), max_length=30)
    def __unicode__(self):
        return self.name
    class Meta:
        ordering = ["name"]
        verbose_name = _('client type')
        verbose_name_plural = _('client types')
    
admin.site.register(ClientType)

class TransactionType(models.Model):
    TYPE_CHOICES = (
        ('C', _('creditor')),
        ('D', _('debtor')),
    )
    name = models.CharField(_('name'), max_length=30)
    code = models.CharField(_('abbreviature'), max_length=2, unique=True)
    type = models.CharField(_('type'), max_length=1, choices=TYPE_CHOICES)
    consecutive = models.CharField(_('consecutive'), max_length=10, blank=True, null=True)
    journal_type = models.ForeignKey(Journal_type, verbose_name=_('journal type'), blank=True, null=True)
    journal = models.CharField(_('journal'), max_length=10, blank=True, null=True)
    def __unicode__(self):
        return self.name
    class Meta:
        ordering = ["id"]
        verbose_name = _('transaction type')
        verbose_name_plural = _('transaction types')
    
admin.site.register(TransactionType)
    
class PaymentType(models.Model):
    name = models.CharField(_('name'), max_length=30)
    currency = models.ForeignKey(Currency, verbose_name=_('currency'))
    def __unicode__(self):
        return self.name
    class Meta:
        ordering = ["name"]
        verbose_name = _('payment type')
        verbose_name_plural = _('payment types')
    
admin.site.register(PaymentType)
    
class Client(Company):
    salesman = models.ForeignKey(Salesman, verbose_name=_('saleman'))
    zone = models.ForeignKey(Zone, verbose_name=_('zone'), blank=True, null=True)
    business = models.ForeignKey(LineOfBusiness, verbose_name=('line of business'), blank=True, null=True)
    type = models.ForeignKey(ClientType, verbose_name=_('type'))
    credit_term = models.IntegerField(_('credit terms'), default=0)
    credit_limit = models.DecimalField(_('credit limit'), max_digits=14, decimal_places=2, blank=True, null=True)
    price_list = models.ForeignKey(ListOfPrices, verbose_name=_('list of prices'), blank=True, null=True)
    account = models.ForeignKey(GlAccount, verbose_name=_('gl account'), blank=True, null=True)
    class Meta:
        verbose_name = _('client')
        verbose_name_plural = _('clients')
    
admin.site.register(Client)
    

class SalesStats(models.Model):
    client = models.ForeignKey(Client, verbose_name=_('client'))
    year = models.SmallIntegerField(_('year'))
    month = models.SmallIntegerField(_('month'))
    invoices = models.IntegerField(_('invoices'))
    sales = models.DecimalField(_('sales'),max_digits=14, decimal_places=2)
    costs = models.DecimalField(_('costs'),max_digits=14, decimal_places=2)

class Document(models.Model):
    operation = models.ForeignKey(TransactionType, verbose_name=_('transaction'), related_name='operation')
    folio = models.CharField(_('folio'), max_length=10)
    date = models.DateTimeField(_('date'), auto_now_add=True)
    aux_oper = models.ForeignKey(TransactionType, verbose_name=_('transaction'), blank=True, null=True)
    aux_fol = models.CharField(_('reference'), max_length=10, blank=True, null=True)
    salesman = models.ForeignKey(Salesman, verbose_name=_('salesman'))
    location = models.ForeignKey(Location, verbose_name=_('location'))
    client = models.ForeignKey(Client, verbose_name=_('client'), related_name='client')
    delivery = models.ForeignKey(Client, verbose_name=_('delivery'), blank=True, null=True, related_name='delivery')
    credit_term = models.IntegerField(_('credit terms'), default=0)
    currency = models.ForeignKey(Currency, verbose_name=_('currency'))
    rate = models.DecimalField(_('rate'), max_digits=14, decimal_places=2, default=1)
    units = models.DecimalField(_('units'), max_digits=14, decimal_places=2, default=0)
    add_amount = models.DecimalField(_('add_amount'), max_digits=14, decimal_places=2, default=0)
    dsct_detail = models.DecimalField(_('detail discount'), max_digits=14, decimal_places=2, default=0)
    dsct_global = models.DecimalField(_('global discount'), max_digits=14, decimal_places=2, default=0)
    charges = models.DecimalField(_('charges'), max_digits=14, decimal_places=2, default=0)
    subtotal = models.DecimalField(_('subtotal'), max_digits=14, decimal_places=2, default=0)
    taxes = models.DecimalField(_('taxes'), max_digits=14, decimal_places=2, default=0)
    total = models.DecimalField(_('total'), max_digits=14, decimal_places=2, default=0)
    status = models.CharField(_('status'), max_length=1, default='N')
    user = models.ForeignKey(User, verbose_name=_('user'))
    cashregister = models.SmallIntegerField(_('cashregister'), default=0)
    shift = models.SmallIntegerField(_('shift'), default=0)
    end_oper = models.TimeField(_('end operation time'), auto_now_add=True)
    balance = models.DecimalField(_('balance'), max_digits=14, decimal_places=2, default=0)
    gl_operation = models.IntegerField(_('gl operation'), default=0)
    im_operation = models.IntegerField(_('im operation'), default=0)
    ac_operation = models.IntegerField(_('ac operation'), default=0)
    
class Detail(models.Model):
    document = models.ForeignKey(Document, verbose_name=_('document'))
    operation = models.IntegerField(_('transaction'), editable=False)
    folio = models.CharField(_('folio'), max_length=10, editable=False)
    date = models.DateTimeField(_('date'), editable=False)
    sequence = models.IntegerField(_('sequence'), editable=False)
    quantity = models.DecimalField(_('quantity'), max_digits=14, decimal_places=2, default=0)
    product = models.ForeignKey(Product, verbose_name=_('product'))
    family = models.CharField(_('family'), max_length=10, editable=False)
    client = models.IntegerField(_('client'),editable=False)
    price = models.DecimalField(_('price'), max_digits=14, decimal_places=2, default=0)
    rate = models.DecimalField(_('rate'), max_digits=14, decimal_places=2, default=1, editable=False)
    tax_percent = models.DecimalField(_('tax percent'), max_digits=5, decimal_places=2)
    salesman = models.IntegerField(_('saleman'), editable=False)
    status = models.CharField(_('status'), max_length=1, default='N', editable=False)
    furnished = models.DecimalField(_('furnished'), max_digits=14, decimal_places=2, default=0, editable=False)
    returned = models.DecimalField(_('returned'), max_digits=14, decimal_places=2, default=0, editable=False)
    backorder = models.DecimalField(_('backorder'), max_digits=14, decimal_places=2, default=0, editable=False)

